from django.shortcuts import HttpResponse
from api.task import x1
from auction import celery_app
from celery.result import AsyncResult
import datetime


def create_task(request):
    # 本地时间
    ctime = datetime.datetime.now()
    # 本地转utc时间
    utc_ctime = datetime.datetime.utcfromtimestamp(ctime.timestamp())
    target_time = utc_ctime + datetime.timedelta(seconds=10)
    result = x1.apply_async(args=[4, 5], eta=target_time)
    # result = x1.delay(4, 5)

    return HttpResponse(result.id)


def get_result(request):
    nid = request.GET.get('nid')
    result_object = AsyncResult(id=nid, app=celery_app)
    if result_object.successful():
        data = result_object.get()
        print(nid, data)
        return HttpResponse(data)
    return HttpResponse("...")
